////////////////////////////////////////////////////////////////////////////////


*Job security in the rest of Europe analysis


////////////////////////////////////////////////////////////////////////////////


////////////////////////////////////////////////////////////////////////////////
*Population-weighted average change in insecurity and unemployment, 2005-2015
////////////////////////////////////////////////////////////////////////////////

cd "$europedata"

import delimited "raw/europe_populations15.csv", clear
	rename (time geo value) (year id population)
	la var id "Country"
	la var year "Year"
	la var population "Population (2015)"
	keep id year population 
	keep if year==2015
	drop year
	replace id="Germany" if id=="Germany (until 1990 former territory of the FRG)"
save "temp/populations.dta", replace

use "clean/insecurity_trends.dta", clear
	keep country year insecure
	decode country, gen(id)
	replace id="Czechia" if id=="Czech Republic"
	replace id="North Macedonia" if id=="FYROM"
	
merge m:1 id using "temp/populations.dta" 
	drop if _merge==2
	drop _merge

merge 1:1 id year using "clean/unemp.dta", keepusing(unemp)
keep if _m==3
drop _m
	
replace population=subinstr(population,",","",.)
destring population, replace
drop country
drop if id=="EU"
reshape wide insecure unemp, i(id) j(year)

gen change_insecurity=insecure2015-insecure2005
gen change_unemp=unemp2015-unemp2005

sum change_insecurity [fw=population]

di "Mean change in insecurity 2005-2015: `r(mean)'" 

sum change_unemp [fw=population]

di "Mean change in unemployment 2005-2015: `r(mean)'" 

////////////////////////////////////////////////////////////////////////////////

*Regress insecurity measure on unemployment, year dummies, and country effects

////////////////////////////////////////////////////////////////////////////////

cd "$europedata"

use "clean/ewcs_clean.dta", clear
decode countid, gen(id)
replace id = "Czechia" if id == "Czech Republic"
replace id = "North Macedonia" if id == "FYROM"

merge m:1 id year using "clean/unemp.dta", keepusing(unemp)
keep if _m==3
drop _merge

replace unemp=unemp/100
rename countid country

logit insecure i.country ib2005.year i.temp i.semp i.parttime c.jobtenure c.age c.age_squared i.uni i.industry i.male c.unemp if insample [pw=xw], vce(cluster year)
margins, dydx(i.year c.unemp) post
estimates store europe_ctrls_fes
estadd local fixed "Yes", replace
estadd local ctrl "Yes", replace

logit insecure ib2005.year i.temp i.semp i.parttime c.jobtenure c.age c.age_squared i.uni i.industry i.male c.unemp if insample [pw=xw], vce(cluster year)
margins, dydx(i.year c.unemp) post
estimates store europe_ctrls_no_fes
estadd local fixed "No", replace
estadd local ctrl "Yes", replace

logit insecure i.country ib2005.year c.unemp if insample [pw=xw], vce(cluster year)
margins, dydx(i.year c.unemp) post
estimates store europe_noctrls_fes
estadd local fixed "Yes", replace
estadd local ctrl "No", replace

logit insecure ib2005.year c.unemp if insample [pw=xw], vce(cluster year)
margins, dydx(i.year c.unemp) post
estimates store europe_noctrls_no_fes
estadd local fixed "No", replace
estadd local ctrl "No", replace

cd "$tables"

////////////////////////////////////////////////////////////////////////////////
*TABLE 7: Europe job security regression results
////////////////////////////////////////////////////////////////////////////////

esttab europe_noctrls_no_fes europe_noctrls_fes europe_ctrls_no_fes europe_ctrls_fes using "europe.tex", keep(unemp 2010.year 2015.year) se mlabel("" "" "" "") coeflabels(unemp "Unemployment rate*100" 2010.year "2010" 2015.year "2015") scalar("fixed Country FEs" "ctrl Other controls" "N N") title("Job insecurity regression results (Europe 2005-2015)") postfoot("\tabnotes{5}{This table displays coefficients and SEs from a logistic regression of the indicator for feeling insecure on the unemployment rate and year dummies, with 2005 as the base year. Demographic and job characteristic controls are included in models (3) and (4), and country fixed effects in (2) and (4). Standard errors in parentheses. *\textit{p} < 0.05, **\textit{p} < 0.01, ***\textit{p} < 0.001}") booktab width(\hsize) replace

////////////////////////////////////////////////////////////////////////////////


*Job satisfaction analysis for US, UK, and Germany


////////////////////////////////////////////////////////////////////////////////

**PART ONE: UK

cd "$ukdata"

use "clean/uk_linear_reverse.dta", clear
xtset id wave

*Correlation between job satisfaction and job security
gen satjob=jbsat if !missing(jbsat)&jbsat>0
gen sample=(insample&!missing(satjob))

corr bhps_security satjob if bhps&sample
di "Correlation between job security and job satisfaction, 1991-2009: `r(rho)'"

corr ukhls_security satjob if !bhps&sample
di "Correlation between job security and job satisfaction, 2010-2018: `r(rho)'"

*Regressions
logit satisfied ib2001.year i.temp i.parttime c.age c.age_squared c.jobtenure i.semp_boss i.semp i.union i.nonwhite i.immigrant i.male ib1.marital ib1.isco i.uni if sample [pw=relative_xw], vce(robust)
	margins, dydx(i.year) saving(temp/dissat_controls.dta, replace)

logit satisfied ib2001.year if sample [pw=relative_xw], vce(robust)
	margins, dydx(i.year) saving(temp/dissat_nocontrols.dta, replace)

use "temp/dissat_nocontrols.dta", clear
	keep _deriv _margin _ci* _se
	rename (_margin _ci_ub _ci_lb _se) (dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols se_nocontrols)

merge 1:1 _deriv using "temp/dissat_controls.dta"
	
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	
	rename (_margin _ci_lb _ci_ub) (dydx_controls ci_lower_controls ci_upper_controls)
	keep year dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols dydx_controls ci_upper_controls ci_lower_controls
	gen year_controls=year+.2
	replace year=year-.2
	set obs 28
	replace dydx_nocontrols=0 in 28
	replace dydx_controls=0 in 28
	replace ci_upper_controls=0 in 28
	replace ci_lower_controls=0 in 28
	replace ci_upper_nocontrols=0 in 28
	replace ci_lower_nocontrols=0 in 28
	replace year=2000.8 in 28
	replace year_controls=2001.2 in 28
	sort year
	
*Plot

cd "$ukgraphics"

twoway (scatter dydx_nocontrols year, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_controls year_controls, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_nocontrols ci_lower_nocontrols year, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_controls ci_lower_controls year_controls, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1991/2017, angle(45)) scale(.8) legend(label(1 "Without controls") label(2 "With controls") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Satisfied)" "UK, 1991-2018") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "bhps_dissat_year_dummies.png", as(png) name("Graph") replace

/*
cap erase "data/temp/dissat_controls.dta"
cap erase "data/temp/dissat_nocontrols.dta"
*/

**PART TWO: US

cd "$usdata"

use "clean/gss_clean.dta", clear
	gen satisfied=1 if satjob==1|satjob==2
	replace satisfied=0 if satjob==3|satjob==4
	recode satjob 1=4 2=3 3=2 4=1
	
*Correlation between job satisfaction and job security
gen sample=insample&!missing(satisfied)
corr satjob joblose if sample
di "Correlation between job satisfaction and job security: `r(rho)'"

*Year dummies without controls	
logit satisfied ib2002.year if sample [pw=wtssall], vce(robust)
	margins, dydx(i.year) saving(temp/dissat_nocontrols.dta, replace)
	
*Year dummies with controls
logit satisfied ib2002.year i.semp i.parttime i.onedigit_naics age age_squared i.male i.union i.uni i.nonwhite i.immigrant ib5.marital if sample [pw=wtssall], vce(robust)
	margins, dydx(i.year) saving(temp/dissat_controls.dta, replace)

*Margins and plot
use "temp/dissat_nocontrols.dta", clear
	keep _deriv _margin _ci* _se
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb _se) (dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols se_nocontrols)
tempfile nocontrols
save `nocontrols'
	
use "temp/dissat_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `nocontrols'
	rename (_margin _ci_lb _ci_ub) (dydx_controls ci_lower_controls ci_upper_controls)
	keep year dydx_nocontrols ci_upper_nocontrols ci_lower_nocontrols dydx_controls ci_upper_controls ci_lower_controls
	gen year_controls=year+.3
	replace year=year-.3
	set obs 32
	replace dydx_nocontrols=0 in 32
	replace dydx_controls=0 in 32
	replace ci_upper_controls=0 in 32
	replace ci_lower_controls=0 in 32
	replace ci_upper_nocontrols=0 in 32
	replace ci_lower_nocontrols=0 in 32
	replace year=2001.7 in 32
	replace year_controls=2002.3 in 32
	sort year
	
*Plot

cd "$usgraphics"

twoway (scatter dydx_nocontrols year, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_controls year_controls, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper_nocontrols ci_lower_nocontrols year, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_controls ci_lower_controls year_controls, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1978(2)2018, angle(45)) scale(.8) legend(label(1 "Without controls") label(2 "With controls") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Satisfied)" "US, 1972-2018") yline(0, lcolor(black*2)) xline(2002, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "gss_dissat_year_dummies.png", as(png) name("Graph") replace

/*
cap erase "temp/dissat_controls.dta"
cap erase "temp/dissat_nocontrols.dta"
*/

**PART THREE: Germany

cd "$germanydata"

use "clean/soep_clean.dta", clear
	gen satisfied=1 if jobsat>=5&!missing(jobsat)
	replace satisfied=0 if jobsat<=4

*Correlation between job satisfaction and job security
gen sample=insample&!missing(satisfied)
corr jobsat jobsecurity if sample
di "Correlation between job satisfaction and job security: `r(rho)'"

*Year dummies without controls	
logit satisfied ib2001.year if sample [pw=xw], vce(robust)
	margins, dydx(i.year) saving(temp/dissat_nocontrols.dta, replace)

*Year dummies with controls
logit satisfied ib2001.year i.temp i.semp i.parttime i.immigrant ib3.marital ib1.isco i.male i.marginal age age_squared i.uni if sample [pw=xw], vce(robust)
	margins, dydx(i.year) saving(temp/dissat_controls.dta, replace)

use "temp/dissat_nocontrols.dta", clear
	keep _deriv _margin _ci*
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace
	rename (_margin _ci_ub _ci_lb) (dydx ci_upper ci_lower)
tempfile nocontrols
save `nocontrols'

use "temp/dissat_controls.dta", clear
	decode _deriv, gen(year)
	replace year=substr(year, 1, 4)
	destring year, replace

merge 1:1 year using `nocontrols'	
	rename (_margin _ci_lb _ci_ub) (dydx_controls ci_lower_controls ci_upper_controls)
	keep year dydx ci_upper ci_lower dydx_controls ci_upper_controls ci_lower_controls
	gen year_controls=year+.15
	replace year=year-.15
	set obs 35
	replace dydx=0 in 35
	replace dydx_controls=0 in 35
	replace ci_upper_controls=0 in 35
	replace ci_lower_controls=0 in 35
	replace ci_upper=0 in 35
	replace ci_lower=0 in 35
	replace year=2000.85 in 35
	replace year_controls=2001.15 in 35
	sort year
	
cd "$germanygraphics"

twoway (scatter dydx year, connect(direct) msize(small) lcolor(gs0) mcolor(gs0)) (scatter dydx_controls year_controls, connect(direct) msize(small) lcolor(gs10) mcolor(gs10)) (rcap ci_upper ci_lower year, msize(0) lcolor(gs0) mcolor(gs0)) (rcap ci_upper_controls ci_lower_controls year_controls, msize(0) lcolor(gs10) mcolor(gs10)), graphregion(color(white)) bgcolor(white) xlabel(1984(2)2018, angle(45)) scale(.8) legend(label(1 "Without controls") label(2 "With controls") label(3 "95% CI") label(4 "95% CI")) title("Marginal effects of year dummies on Pr(Satisfied)" "Germany, 1984-2018") yline(0, lcolor(black*2)) xline(2001, lcolor(gs0) lpattern(dash)) ytitle("Marginal effect")

graph export "soep_dissat_year_dummies.png", as(png) name("Graph") replace

/*
cap erase "temp/dissat_controls.dta"
cap erase "temp/dissat_nocontrols.dta"
*/

cd "$ukdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

cd "$usdata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

cd "$germanydata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}

cd "$europedata/temp"
local files: dir "`c(pwd)'" files "*"

foreach file of local files {
	cap erase `file'
}
